package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.RegBaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.net.PwdReset;
import com.meihua.baoleyi.net.ResetClass;
import com.meihua.baoleyi.utils.StringUtils;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.view.ResetPwdView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

import static com.meihua.baoleyi.utils.StringUtils.isOrderNumeric;
import static com.meihua.baoleyi.utils.StringUtils.isOrderNumeric_;

/**
 * Name: liming
 * Time: 2017/10/16.
 * 重设密码
 */

public class ResetPwdActivity extends RegBaseActivity implements ResetPwdView, View.OnClickListener {
    @Bind(R.id.left_img)
    RelativeLayout leftImg;
    @Bind(R.id.center_text)
    TextView centerText;
    @Bind(R.id.right_text)
    TextView rightText;
    private ResetClass resetPresenter;
    private EditText reset_pwd;
    private EditText reset_config_pwd;
    private Button reset_pwd_submit;
    String password1,password2;
    String phoneNum;
    @Override
    public int getLayoutResource() {
        return R.layout.activity_reset_pwd;
    }
    @Override
    public void onInitView() {
        phoneNum= getIntent().getStringExtra("user_phone");
        reset_pwd = (EditText) findViewById(R.id.reset_pwd);
        reset_config_pwd = (EditText) findViewById(R.id.reset_config_pwd);
        resetPresenter = new PwdReset(this, this);
        findViewById(R.id.reset_pwd_submit).setOnClickListener(this);
    }
    @Override
    public void onReset(String newspass1, String newspass2, String msg, boolean isSuccess) {

        if (isSuccess) {
            finish();
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset_pwd_submit:
                 password1 = reset_pwd.getText().toString().trim();
                 password2 = reset_config_pwd.getText().toString().trim();
                if (password1.length() < 8) {
                    Toast.makeText(ResetPwdActivity.this, "密码长度不小于8", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isAllNum(password1)) {
                    Toast.makeText(ResetPwdActivity.this, "不能输入纯数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isAllCode(password1)) {
                    Toast.makeText(ResetPwdActivity.this, "不能输入纯字母", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.equalStr(password1)) {
                    Toast.makeText(ResetPwdActivity.this, "不能全是相同的数字或者字母", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isOrderNumeric(password1) || isOrderNumeric_(password1)) {
                    Toast.makeText(ResetPwdActivity.this, "不能是连续的数字--递增或递减", Toast.LENGTH_SHORT).show();
                    return;
                }
                RestPwd();//重置密码
                break;
        }
    }

    private void RestPwd() {
        OkGo.post(APIServer.RESET_PASSWORD)
                .tag(this)
                .params("user_phone",phoneNum)
                .params("re_password",password1)
                .params("new_password",password2)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s != null){
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.optString("code").equals("200")) {
//                                    ToastUtil.showToast(ResetPwdActivity.this,jsonObject.optString("msg"), 200);
                                    Intent intent = new Intent(ResetPwdActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    ToastUtil.show(ResetPwdActivity.this, "密码修改成功");
                                }else {
                                    ToastUtil.show(ResetPwdActivity.this, jsonObject.optString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        centerText.setText("重置密码");
        rightText.setVisibility(View.GONE);
    }

    @OnClick(R.id.left_img)
    public void onViewClicked() {
        finish();
    }
}
